home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / objtools / tlfit.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  165 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /* 
  18.  *    tlfit -
  19.  *        Fit a triangle list to a -1,1 -1,1 space.
  20.  *
  21.  *            Paul Haeberli - 1990
  22.  */
  23. #include "math.h"
  24. #include "stdio.h"
  25. #include "vect.h"
  26. #include "trilist.h"
  27.  
  28. main(argc,argv)
  29. int argc;
  30. char **argv;
  31. {
  32.     int zonly;
  33.  
  34.     if(argc<3) {
  35.     fprintf(stderr,"tlfit: in.tl out.tl [-z]\n");
  36.     exit(1);
  37.     }
  38.     if(argc>3)
  39.     zonly = 1;
  40.     else
  41.     zonly = 0;
  42.     tlfit(argv[1],argv[2],-0.95,0.95,-0.95,0.95,-1.0,1.0,zonly);
  43.     exit(0);
  44. }
  45.  
  46. tlfit(inname,outname,xmin,xmax,ymin,ymax,zmin,zmax,zonly)
  47. char *inname, *outname;
  48. float xmin,xmax,ymin,ymax,zmin,zmax;
  49. int zonly;
  50. {
  51.     float delx, dely, delz;
  52.     float odelx, odely;
  53.     float tdelx, tdely;
  54.     float oxmin, oxmax;
  55.     float oymin, oymax;
  56.     float ozmin, ozmax;
  57.     float xmid, ymid;
  58.     float xavg, yavg;
  59.     float sc, zsc;
  60.     int i, ntri, mode;
  61.     trilisttri tri;
  62.     short magic;
  63.     FILE *outf;
  64.  
  65.     delx = xmax-xmin;
  66.     dely = ymax-ymin;
  67.     delz = zmax-zmin;
  68.  
  69.     oxmin = oymin = ozmin = 1000.0;
  70.     oxmax = oymax = ozmax = -1000.0;
  71.     ntri = tlopen(inname);
  72.     for(i=0; i<ntri; i++) {
  73.     tlread(&tri);
  74.  
  75.     if(oxmin>tri.x0)
  76.         oxmin=tri.x0;
  77.     if(oxmax<tri.x0)
  78.         oxmax=tri.x0;
  79.     if(oymin>tri.y0)
  80.         oymin=tri.y0;
  81.     if(oymax<tri.y0)
  82.         oymax=tri.y0;
  83.     if(ozmin>tri.z0)
  84.         ozmin=tri.z0;
  85.     if(ozmax<tri.z0)
  86.         ozmax=tri.z0;
  87.  
  88.     if(oxmin>tri.x1)
  89.         oxmin=tri.x1;
  90.     if(oxmax<tri.x1)
  91.         oxmax=tri.x1;
  92.     if(oymin>tri.y1)
  93.         oymin=tri.y1;
  94.     if(oymax<tri.y1)
  95.         oymax=tri.y1;
  96.     if(ozmin>tri.z1)
  97.         ozmin=tri.z1;
  98.     if(ozmax<tri.z1)
  99.         ozmax=tri.z1;
  100.  
  101.     if(oxmin>tri.x2)
  102.         oxmin=tri.x2;
  103.     if(oxmax<tri.x2)
  104.         oxmax=tri.x2;
  105.     if(oymin>tri.y2)
  106.         oymin=tri.y2;
  107.     if(oymax<tri.y2)
  108.         oymax=tri.y2;
  109.     if(ozmin>tri.z2)
  110.         ozmin=tri.z2;
  111.     if(ozmax<tri.z2)
  112.         ozmax=tri.z2;
  113.     }
  114.     tlclose();
  115.     tdelx = odelx = oxmax-oxmin;
  116.     tdely = odely = oymax-oymin;
  117.     tdelx /= delx;
  118.     tdely /= dely;
  119.     if(tdelx>tdely)
  120.     mode = 1;
  121.     else 
  122.     mode = 2;
  123.     switch(mode) {
  124.     case 1:
  125.         sc = delx/odelx;
  126.         break;
  127.     case 2:
  128.         sc = dely/odely;
  129.         break;
  130.     }
  131.     zsc = delz/(ozmax-ozmin);
  132.     xmid = (oxmin+oxmax)/2.0;
  133.     ymid = (oymin+oymax)/2.0;
  134.     xavg = (xmin+xmax)/2.0;
  135.     yavg = (ymin+ymax)/2.0;
  136.  
  137.     ntri = tlopen(inname);
  138.     outf = fopen(outname,"w");
  139.     magic = TLMAGIC;
  140.     fwrite(&magic,sizeof(short),1,outf);
  141.     for(i=0; i<ntri; i++) {
  142.     tlread(&tri);
  143.     if(zonly) {
  144.         tri.z0 = zmin+zsc*(tri.z0-ozmin);
  145.         tri.z1 = zmin+zsc*(tri.z1-ozmin);
  146.         tri.z2 = zmin+zsc*(tri.z2-ozmin);
  147.     } else {
  148.         tri.x0 = xavg+(sc*(tri.x0-xmid));
  149.         tri.y0 = yavg+(sc*(tri.y0-ymid));
  150.         tri.z0 = zmin+zsc*(tri.z0-ozmin);
  151.  
  152.         tri.x1 = xavg+(sc*(tri.x1-xmid));
  153.         tri.y1 = yavg+(sc*(tri.y1-ymid));
  154.         tri.z1 = zmin+zsc*(tri.z1-ozmin);
  155.  
  156.         tri.x2 = xavg+(sc*(tri.x2-xmid));
  157.         tri.y2 = yavg+(sc*(tri.y2-ymid));
  158.         tri.z2 = zmin+zsc*(tri.z2-ozmin);
  159.     }
  160.     fwrite(&tri,sizeof(trilisttri),1,outf);
  161.     }
  162.     tlclose();
  163.     fclose(outf);
  164. }
  165.